<?php
include_once '../BaseService.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/db/AppInfoDAO.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/util/Util.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/db/UserDAO.php';

class AppInfoService extends BaseService {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
		$this->log = Plog::factory ( __FILE__ );
	}
	
	public function login() {
		$account = $_POST ["account"];
		$password = $_POST ['password'];
		$userDAO = new UserDAO ();
		
		$user = $userDAO->getUser ( $account, $password );
		
		if ($user == null) { //無帳號
			header ( "Location:/ad/html/publish/publishLogin.php?error=1&account=$account&password=$password" );
		} else if ($user->Type == 3) { //開發人員
			@session_start ();
			$_SESSION ["user"] = Util::toJson ( $user );
			header ( "Location:/ad/html/publish/publishAccount.php" );
		} else { //一般使用者
			@session_start ();
			$_SESSION ["user"] = Util::toJson ( $user );
			header ( "Location:/ad/html/publish/publishLogin.php?error=2&account=$account" );
		}
	
	}
	
	/**
	 * 
	 * 開者者 新增appInfo
	 */
	public function trgetInsert() {
		try {
			@session_start ();
			$user = Util::jsonToObject ( $_SESSION ["user"] );
			// echo $_SESSION["user"];
			$nowDate = Util::getNowDate ();
			$param = array ("name" => $_REQUEST ["name"], "adcategorId" => $_REQUEST ["adcategorId"], "description" => $_REQUEST ["description"], "appType" => $_REQUEST ["appType"], "testAction" => $_REQUEST ["testAction"], "account" => $user->Account, "createTime" => $nowDate, "ModifyTime" => $nowDate, "md5key" => md5 ( $_REQUEST ["name"] . $nowDate ), "status" => 1 );
			
			$appInfoDAO = new AppInfoDAO ();
			$result = $appInfoDAO->getAppInfoByName($_REQUEST ["name"]);
			if($result!="")
			{
				$msg="該軟體名稱已被註冊!";
				echo "{success:true,result:'$result',msg :'$msg'}";
				return;
			}
			$result = $appInfoDAO->insert ( $param );
			$msg = $result == "" ? "該軟體已被註冊!" : "新增成功";
			if($result!="")
			{
				$userDAO = new UserDAO();
				$sends =  $userDAO->getMenagerUserAccount();
				$fdate = Util::getNowDate('Y/m/d');
				Util::email(
					$sends,
					"$user->Name 新增一個應用程式", 
					"管理者您好，<BR>
				     $user->Name 於$fdate 建立了一個應用程式，<BR>
					  請至<a href='http://mmedia.ai-ad.net.tw/ad/html/publish/publishLogin.php'>http://mmedia.ai-ad.net.tw/ad/html/publish/publishLogin.php</a> 進行確認<BR>
					<BR>
					  謝謝您，<BR>
					 Amazing Mobile Ads敬上<BR>
					"
				);
				
			}
			echo "{success:true,result:'$result',msg :'$msg'}";
			//Util::forward("/ad/html/publish/publishMAppList.php");
		} catch ( Exception $e ) {
			echo "{failure :true,result:'0', msg:'$e->getMessage()'}";
		}
	}
	
	/**
	 * 取得發行者的程式清單
	 * Enter description here ...
	 */
	public function getAppInfoList() {
		$account = null;
		if (isset ( $_REQUEST ["account"] ) && strlen ( $_REQUEST ["account"] ) > 1 && $_REQUEST ["account"] != "undefined") {
			//		echo $account."<-->>>";
			$account = $_REQUEST ["account"];
		} else {
			@session_start ();
			$user = Util::jsonToObject ( $_SESSION ["user"] );
			$account = $user->Account;
		}
		//		echo $account."<--";
		if (isset ( $_REQUEST ["start"] ))
			$start = $_REQUEST ["start"];
		if (isset ( $_REQUEST ["limit"] ))
			$limit = $_REQUEST ["limit"];
			
//		echo "start=$start,limit=$limit";
		$appInfoDAO = new AppInfoDAO ();
		$result = $appInfoDAO->getAppInfoList ( $start, $limit, $account );
		
		echo "{count:" . $result ["count"] . ",row:" . Util::toJson ( $result ["item"] ) . "}";
	}
	/**
	 * 取得發行者的程式detail
	 * Enter description here ...
	 */
	public function getAppInfo() {
		$id = $_GET ["id"];
		
		$appInfoDAO = new AppInfoDAO ();
		$result = $appInfoDAO->getAppInfo ( $id );
		
		echo Util::toJson ( $result );
	}
	/**
	 * 取得發行者的程式detail
	 * Enter description here ...
	 */
	public function getAppInfoByName() {
		$name = $_GET ["name"];
		
		$appInfoDAO = new AppInfoDAO ();
		$result = $appInfoDAO->getAppInfoByName ( $name );
		
		echo Util::toJson ( $result );
	}
	
	public function updateAppInfo() {
		session_start ();
		//$user = Util::jsonToObject($_SESSION["user"]);
		

		$appInfoDAO = new AppInfoDAO ();
		
		$reuslt = $appInfoDAO->updateAppInfo ( //$user->Account,
		$_POST ["id"], $_POST ["name"], $_POST ["adcategorId"], $_POST ["description"], $_POST ["appType"], $_POST["status"],
		$_POST ["testAction"] );
		
		if ($reuslt == 1) {
			echo "{result :1}";
		} else {
			
			echo "{result :0}";
		}
	}
	public function getAppInfoRate() {
		@session_start ();
		$user = Util::jsonToObject ( $_SESSION ["user"] );
		$start = $_REQUEST ["start"];
		$limit = $_REQUEST ["limit"];
		$appInfoDAO = new AppInfoDAO ();
		
		$result = $appInfoDAO->getAppInfoRate ( $user->Account, $start, $limit );
		
		echo "{count:" . sizeof ( $result [0] ) . ",row:" . str_replace ( "null", 0, Util::toJson ( $result [0] ) ) . "}";
	
		//echo Util::toJson($result);
	}
	
	public function getAppInfoDetail() {
		@session_start();
		$id=$_REQUEST ["id"];
		$appInfoDAO = new AppInfoDAO ();
		$_SESSION["appInfoDetail"]=Util::toJson($appInfoDAO->getAppInfo ($id ));
		header("Location:/ad/html/publish/publishMAppView.php?id=$id");
//		echo  "{row:".Util::toJson($result)."}";
	
		//echo Util::toJson($result);
	}
}

$service = new AppInfoService ();
$service->__call ( $_GET ["action"], null );
//$service->queryTargetAccountMember();
?>